package com.lagou.server;

import com.lagou.model.Question;
import com.lagou.model.QuestionMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.List;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;
    private List<User> list;
    private List<Question> questions;
    private ServerQuestionDao sqd;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd,List<User> list,List<Question> questions,ServerQuestionDao sqd) {
        this.sic = sic;
        this.sd = sd;
        this.list=list;
        this.questions = questions;
        this.sqd =sqd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException ,ClassNotFoundException {
        while (true) {
            System.out.println("服务器等待数据的到来...");
            // 使用对象输入流来接收
            //UserMessage tum = (UserMessage) sic.getOis().readObject();
            Object obj = sic.getOis().readObject();
            System.out.println("服务器接收到的对象是：" + obj);
            if (obj instanceof UserMessage) {
                UserMessage userMessage = (UserMessage) obj;
                // 使用分支结构针对客户端发来的不同类型进行不同的处理
                switch (userMessage.getType()) {
                    case "managerCheck":
                        serverManagerCheck(userMessage);
                        break;
                    case "userCheck":
                        addUser(userMessage);
                        break;
                    case "queryUser":
                        queryUser(userMessage);
                        break;
                    case "deleteUser":
                        deleteUser(userMessage);
                        break;
                    case "updateUser":
                        updateUser(userMessage);
                        break;
                    case "studentCheck":
                        serverStudentCheck(userMessage);
                        break;
                    case "updatePassword":
                        updatePassword(userMessage);
                        break;
                    case "quit":
                        quit(userMessage);
                        break;
                }
            }
            if (obj instanceof QuestionMessage) {
                QuestionMessage questionMessage = (QuestionMessage) obj;
                switch (questionMessage.getType()) {
                    case "addQuestion":
                        addQuestion(questionMessage);
                        break;
                    case "queryQuestion":
                        queryQuestion(questionMessage);
                        break;
                    case "deleteQuestion":
                        deleteQuestion(questionMessage);
                        break;
                    case "updateQuestion":
                        updateQuestion(questionMessage);
                        break;
                    case "quitQuestion":
                        quitQuestion(questionMessage);
                        break;
                }
            }


        }
    }

    private void quitQuestion(QuestionMessage questionMessage) throws IOException {
        sqd.writeUserFile();
        questionMessage.setType("success");
        sic.getOos().writeObject(questionMessage);
    }

    private void quit(UserMessage userMessage) throws IOException {
        sd.writeUserFile();
        userMessage.setType("success");
        sic.getOos().writeObject(userMessage);
    }

    private void updatePassword(UserMessage userMessage) throws IOException, ClassNotFoundException {
        // 调用成员方法读取所有学员信息并打印
        list = sd.readUserFile();
        System.out.println("读取到的学员账户信息有：");
        for(User user: list) {
            if( user.getUserName().equals(userMessage.getUser().getUserName())){
                user.setPassword(userMessage.getUser().getPassword());
                userMessage.setType("success");
                userMessage.setUser(user);
                sic.getOos().writeObject(userMessage);
                System.out.println(user);
            }else {
                userMessage.setType("fail");
            }
        }
    }


    private void updateQuestion(QuestionMessage questionMessage) {

    }

    private void deleteQuestion(QuestionMessage questionMessage) {

    }

    private void queryQuestion(QuestionMessage questionMessage) throws IOException, ClassNotFoundException {
        ServerQuestionDao serverQuestionDao = new ServerQuestionDao(questions);
        questions = serverQuestionDao.readUserFile();
        System.out.println("读取到的学员账户信息有：");
        for(Question question: questions) {
            if( question.getTitle().contains(questionMessage.getQuestion().getTitle())){
                questionMessage.setType("success");
                questionMessage.setQuestion(question);
                sic.getOos().writeObject(questionMessage);
                System.out.println(question);
            }else {
                questionMessage.setType("fail");
            }
        }
    }

    private void addQuestion(QuestionMessage questionMessage) throws IOException {
        System.out.println("接收到的消息是：" + questionMessage);
        for(Question question: questions) {
            if(question.getTitle().equalsIgnoreCase(questionMessage.getQuestion().getTitle())) {
                questionMessage.setType("fail");
                sic.getOos().writeObject(questionMessage);
                System.out.println("服务器发送添加考题结果失败！");
                return;
            }
        }
        // 若不存在则调用集合中add方法添加即可
        questions.add(questionMessage.getQuestion());
        questionMessage.setType("success");
        sic.getOos().writeObject(questionMessage);
        System.out.println("服务器发送添加考题成功！");
    }

    private void updateUser(UserMessage userMessage) throws IOException, ClassNotFoundException {
        // 调用成员方法读取所有学员信息并打印
        System.out.println("读取到的学员账户信息有：");
        for(User user: list) {
            if( user.getUserName().equals(userMessage.getUser().getUserName())){
                user.setPassword(userMessage.getUser().getPassword());
                user.setUserName(userMessage.getUser().getUserName());
                userMessage.setType("success");
                userMessage.setUser(user);
                int i = list.indexOf(user);
                list.set(i,user);
                sic.getOos().writeObject(userMessage);
                System.out.println(user);
            }else {
                userMessage.setType("fail");

            }
        }
    }

    private void deleteUser(UserMessage userMessage) throws IOException, ClassNotFoundException {
        // 调用成员方法读取所有学员信息并打印

        System.out.println("读取到的学员账户信息有：");
        for(User user: list) {
            if( user.getUserName().equals(userMessage.getUser().getUserName())){
                list.remove(user);
                userMessage.setType("success");
                sic.getOos().writeObject(userMessage);
            }else {
                userMessage.setType("fail");
            }
        }
    }

    private void queryUser(UserMessage userMessage) throws IOException, ClassNotFoundException {
        // 调用成员方法读取所有学员信息并打印
        //list = sd.readUserFile();
        System.out.println("读取到的学员账户信息有：");
        for(User user: list) {
           if( user.getUserName().equals(userMessage.getUser().getUserName())){
               userMessage.setType("success");
               userMessage.setUser(user);
               sic.getOos().writeObject(userMessage);
               System.out.println(user);
           }else {
               userMessage.setType("fail");
           }
        }

    }

    /**
     * 管理员校验
     * @param userMessage
     * @throws IOException
     */
    public void serverManagerCheck(UserMessage userMessage) throws IOException {
        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(userMessage.getUser())) {
            userMessage.setType("success");
        } else {
            userMessage.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(userMessage);
        System.out.println("服务器发送校验结果成功！");
    }
    public void serverStudentCheck(UserMessage userMessage) throws IOException, ClassNotFoundException {
        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverStudentCheck(userMessage.getUser())) {
            userMessage.setType("success");
        } else {
            userMessage.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(userMessage);
        System.out.println("服务器发送校验结果成功！");
    }


    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void addUser(UserMessage userMessage) throws IOException, ClassNotFoundException {
        System.out.println("接收到的消息是：" + userMessage);
        for(User user: list) {
            if(user.getUserName().equalsIgnoreCase(userMessage.getUser().getUserName())) {
                userMessage.setType("fail");
                sic.getOos().writeObject(userMessage);
                System.out.println("服务器发送添加学员结果失败！");
                return;
            }
        }
        // 若不存在则调用集合中add方法添加即可
        list.add(userMessage.getUser());
        userMessage.setType("success");
        sic.getOos().writeObject(userMessage);
        System.out.println("服务器发送添加学员结果成功！");
    }
}
